package com.wyl.mine.unique.credit

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.databinding.CommonListActivityBinding
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.binding.recyclerview.RecyclerViewDivider
import com.wyl.libbase.extend.toast
import com.wyl.mine.R
import io.ditclear.bindingadapter.SingleTypeAdapter
import org.koin.android.viewmodel.ext.android.viewModel

class CreditListActivity : BindingActivity<CommonListActivityBinding>() {

    private val viewModel: CreditListViewModel by viewModel()

    override fun getLayoutId(): Int = R.layout.common_list_activity

    override fun initView() {
        binding.vm = viewModel

        binding.titleBar.apply {
            centerTextView.text = "信用明细"
            setListener { _, action, _ ->
                when (action) {
                    CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                    else -> {
                    }
                }
            }
        }

        binding.ad.visibility = View.VISIBLE

        binding.include.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
            addItemDecoration(mDivider)
        }
    }

    private val mAdapter by lazy {
        SingleTypeAdapter(this, R.layout.credit_list_item, viewModel.dataSource)
    }

    private val mDivider by lazy {
        RecyclerViewDivider(this, LinearLayoutManager.VERTICAL)
    }

    override fun loadData() {
        viewModel.refresh()
        viewModel.error.observe(this, Observer { toast(it) })
    }

}
